package com.hinnotek.amoeba.ui.activity.old

import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lee.common.base.BaseActivity
import com.hinnotek.amoeba.R
import com.hinnotek.amoeba.adapters.old.ImageAdapter
import com.hinnotek.amoeba.adapters.old.WipeOutDetail2Adapter
import com.hinnotek.amoeba.base.BaseBean
import com.hinnotek.amoeba.bean.old.AmibaReimburseByIdBean
import com.hinnotek.amoeba.databinding.ActivityWipeOutDetailBinding
import com.hinnotek.amoeba.dialog.CustomDialog
import com.hinnotek.amoeba.utilities.IntentUtils
import com.hinnotek.amoeba.utilities.ToastUtils
import com.hinnotek.amoeba.viewmodels.AmibaReimburseViewModel
import com.lee.common.network.net.ObserverState
import dagger.hilt.android.AndroidEntryPoint
import java.util.*

@AndroidEntryPoint
class WipeOutDetailActivity : BaseActivity<ActivityWipeOutDetailBinding>(), View.OnClickListener {
    private var adapter: WipeOutDetail2Adapter? = null
    private var mAdapter: ImageAdapter? = null
    private var amibaReimburseId = 0
    private var amibaReimburseByIdBean: AmibaReimburseByIdBean? = null
    private var purchaseTypeTo: String? = null

    val viewModel by viewModels<AmibaReimburseViewModel>()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setLeftVisible(true)
        setTitle("报销申请")

        setContentView(R.layout.activity_wipe_out_detail)
        initVew()
    }

    override fun initVew() {

        bindingView.superButtonModify.setOnClickListener(this)
        bindingView.superButtonSubmit.setOnClickListener(this)
        bindingView.superButtonRevoke.setOnClickListener(this)
        val type = intent.getIntExtra("type", 0)
        if (type == 0) {
            bindingView.llWoChooseUnit.visibility = View.GONE
        } else {
            bindingView.llWoChooseUnit.visibility = View.VISIBLE
        }
        setData()
    }

    override fun setData() {
        purchaseTypeTo = intent.getStringExtra("purchase_type_to")
        val is_msg = intent.getBooleanExtra("is_msg", false)
        if (!is_msg) {
            if (purchaseTypeTo == "market" || purchaseTypeTo == "rD") {
                bindingView.llSuperButtonRevoke.visibility = View.VISIBLE
            }
        }
        val manager = LinearLayoutManager(mContext, RecyclerView.VERTICAL, false)

        bindingView.rlvWoDetails.layoutManager = manager
        bindingView.rlvWoDetails.adapter = WipeOutDetail2Adapter().also { adapter = it }
        val manager1 = GridLayoutManager(mContext, 3, RecyclerView.VERTICAL, false)
        bindingView.rlvWoEvidence.layoutManager = manager1
        bindingView.rlvWoEvidence.adapter = ImageAdapter().also { mAdapter = it }
        amibaReimburseId = intent.getIntExtra("amibaReimburseId", 0)
    }

    override fun onResume() {
        super.onResume()
        getAmibaReimburseById(amibaReimburseId)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.superButton_modify -> {
                val bundle = Bundle()
                bundle.putSerializable("detail", amibaReimburseByIdBean)
                bundle.putString("purchase_type_to", purchaseTypeTo)
                IntentUtils.startActivity(mContext, WipeOutModifyActivity::class.java, bundle)
            }
            R.id.superButton_submit -> resubmitAmibaReimburse(amibaReimburseId)
            R.id.superButton_revoke -> backAmibaReimburse(amibaReimburseId)
        }
    }

    /**
     * app端查询单个付款申请详情接口
     */
    fun getAmibaReimburseById(amibaReimburseId: Int) {

        val customDialog = CustomDialog(mContext, "正在加载...")
        customDialog.show()

        viewModel.getAmibaReimburseById(amibaReimburseId)
            .observe(this, object : ObserverState<AmibaReimburseByIdBean>() {
                override fun onDataChange(data: AmibaReimburseByIdBean) {
                    bindingView.tvWoType.text = if (data.amibaReimburseType == 1) "产品" else "项目"
                    if (data.amibaReimburseType == 1) {
                        bindingView.llWoChooseUnit.visibility = View.GONE
                        bindingView.tvWoChooseProject.text = data.provideProjectName
                    } else {
                        bindingView.llWoChooseUnit.visibility = View.VISIBLE
                        bindingView.tvWoChooseUnit.text = data.customerName
                        bindingView.tvWoChooseProject.text = data.projectName
                    }
                    bindingView.tvWoApplicant.text = data.createUser
                    bindingView.tvWoApprover.text = data.approvalUser
                    if (data.approvalState == 1) {
                        bindingView.tvApproverStatus.text = "已申请"
                        bindingView.tvApproverStatus.setTextColor(getColor21(R.color.black))
                    } else if (data.approvalState == 2) {
                        bindingView.tvApproverStatus.text = "已同意"
                        bindingView.tvApproverStatus.setTextColor(getColor21(R.color.xui_btn_green_normal_color))
                    } else if (data.approvalState == 3) {
                        bindingView.tvApproverStatus.text = "已拒绝"
                        bindingView.tvApproverStatus.setTextColor(getColor21(R.color.xui_config_color_red))
                        bindingView.llReject.visibility = View.VISIBLE
                        bindingView.tvReject.text = data.refusalReasons
                    } else if (data.approvalState == 4) {
                        bindingView.tvApplicantStatus.setText("已撤回")
                        bindingView.tvApplicantStatus.setTextColor(getColor21(R.color.xui_config_color_red))
                    }
                    adapter?.setList(data.amibaReimburseProjects)
                    val list1: MutableList<String> = ArrayList()
                    for (amibaReimbursePictures in data.amibaReimbursePictures) {
                        list1.add(amibaReimbursePictures.pictureUrl)
                    }
                    mAdapter?.setList(list1)
                    bindingView.tvWoCc.text = data.sendUser
                    if (data.approvalState == 1) {
                        bindingView.superButtonRevoke.visibility = View.VISIBLE
                        bindingView.superButtonModify.visibility = View.GONE
                        bindingView.superButtonSubmit.visibility = View.GONE
                    } else if (data.approvalState == 3 || data.approvalState == 4) {
                        bindingView.superButtonRevoke.visibility = View.GONE
                        bindingView.superButtonModify.visibility = View.VISIBLE
                        bindingView.superButtonSubmit.visibility = View.VISIBLE
                    }
                    super.onDataChange(data)
                }

                override fun onError(msg: String?, code: Int) {
                    customDialog.dismiss()
                    ToastUtils.showShort(mContext, msg)
                    super.onError(msg, code)
                }
            })
    }

    /**
     * 撤回阿米巴申请单接口
     */
    private fun backAmibaReimburse(amibaReimburseId: Int) {
        val customDialog = CustomDialog(mContext, "正在加载...")
        customDialog.show()

        viewModel.backAmibaReimburse(amibaReimburseId)
            .observe(this, object : ObserverState<BaseBean>() {
                override fun onDataChange(data: BaseBean) {
                    customDialog.dismiss()
                    getAmibaReimburseById(amibaReimburseId)
                    super.onDataChange(data)
                }

                override fun onError(msg: String?, code: Int) {
                    customDialog.dismiss()
                    ToastUtils.showShort(mContext, msg)
                    super.onError(msg, code)
                }
            })
    }

    /**
     * 重新提交报销申请单接口
     */
    private fun resubmitAmibaReimburse(amibaReimburseId: Int) {
        val customDialog = CustomDialog(mContext, "正在加载...")
        customDialog.show()

        viewModel.resubmitAmibaReimburse(amibaReimburseId)
            .observe(this, object : ObserverState<BaseBean>() {
                override fun onDataChange(data: BaseBean) {
                    customDialog.dismiss()
                    finishActivity()
                    super.onDataChange(data)
                }

                override fun onError(msg: String?, code: Int) {
                    customDialog.dismiss()
                    ToastUtils.showShort(mContext, msg)
                    super.onError(msg, code)
                }
            })
    }
}